import {createApp} from 'vue'
import App from './App.vue'

// Vuetify
import 'vuetify/styles' //'vuetify/styles' should not be used in sass files as it resolves to precompiled css
import {createVuetify} from 'vuetify'
import { pl, zhHans } from 'vuetify/locale'
import * as directives from 'vuetify/directives'
import '@mdi/font/css/materialdesignicons.css'
import * as components from 'vuetify/components'
import * as labsComponents from 'vuetify/labs/components'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import 'vant/lib/index.css';

// Router
import {createRouter, createWebHashHistory} from 'vue-router'
import Login from "./Login.vue";
import Backend from "./Backend.vue";
import Users from "./Users.vue";
import Role from "./role.vue";
import Mobile from "./Mobile.vue";
import NotFound from "./notFound.vue"
import Home from "./home.vue"
import SysDict from "./sysDict.vue"
import Bas from "./bas.vue"
import AttackCompany from './attackCompany.vue'
import DefendCompany from './defendCompany.vue'
import Activity from './activity.vue'
import List from './list.vue'
import Battle from './battle.vue'
import Apply from './apply.vue'
import Report from './report.vue'

// Axios
import axios from 'axios'
import {snackbar} from "./store";

const vuetify = createVuetify({
    directives,
    components: {
        ...components,
        ...labsComponents,

    },
    locale: {
        locale: 'zhHans'
      },
    theme: {
        themes: {
            light: {
                dark: false,
                colors: {
                    primary: '#0071EB',
                    background: '#f5f5f7'
                }
            },
        },
    }
})

export const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/backend',
            component: Backend,
            redirect: '/users',
            children: [
                {path: '/users', component: Users},
                {path: '/role', component: Role},
                {path: '/home', component: Home},
                {path: '/sysDict', component: SysDict},
                {path: '/bas', component: Bas},
                {path: '/attackCompany', component: AttackCompany},
                {path: '/defendCompany', component: DefendCompany},
                {path: '/activity', component: Activity},
                {path: '/list', component: List},
                {path: '/battle', component: Battle},
                {path: '/apply', component: Apply},
                {path: '/report', component: Report}
            ]
        },
        {
            path: '/mobile',
            component: Mobile
        },
        {path: '/login', component: Login},
        {path: '/:pathMatch(.*)*', component: NotFound}
    ]
})
// Axios
// 第一医院224
// axios.defaults.baseURL = 'http://192.192.1.224:8092/smartHospital';
// 第二医院21
// axios.defaults.baseURL = 'http://10.10.10.49:8092/smartHospital';
axios.defaults.baseURL = 'http://101.68.222.195:8099/netDefense';
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';

axios.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers['Authorization'] = 'Bearer ' + token;
    }
    return config;
})

axios.interceptors.response.use(res => {
    switch (res.data.code) {
        case 200:
            return res.data;
        case 401:
            snackbar.info('认证失败,请重新登录')
            localStorage.removeItem('token')
            router.push('/login?type=' + JSON.parse(localStorage.getItem("ZAYY_USER_INFO")).userType)
        break
        default:
            snackbar.info(res.data.message)
            return Promise.reject(res);
    }
}, err => {
    if (err.data) {
        switch (err.data.status) {
            case 401:
                snackbar.info('认证失败,请重新登录')
                localStorage.removeItem('token')
                router.push('/login?type=' + JSON.parse(localStorage.getItem("ZAYY_USER_INFO")).userType)
                break
            case 403:
                snackbar.info('无权限')
                break
            case 500:
                snackbar.info(err.data.message)
                break
            default:
                snackbar.info(err.data.status + err.data.message)
        }
    }
    return Promise.reject(err);
})

createApp(App).use(ElementPlus).use(router).use(vuetify).mount('#app')

